<?php
/**
 * Created by PhpStorm.
 * User: EZ
 * Date: 2017/6/19
 * Time: 13:53
 */
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller{
    public function __construct()
    {
        parent::__construct();
        if(session('uid')){
            redirect(U('index/index'));
        }
    }

    public function index(){
        redirect(U('login'));

    }
    public function login(){

        if($_POST){
            $verify = isset($_POST['code']) ? trim($_POST['code']) : '';
            if (!$this->check_verify($verify, 'login')) {
               die(json_error('验证码错误'));
            }
            $username = isset($_POST['name']) ? trim($_POST['name']) : '';
            $password = isset($_POST['pwd']) ? password(trim($_POST['pwd'])) : '';
            $remember = isset($_POST['remember']) ? $_POST['remember'] : 0;
            $username ||  die(json_error('用户名不能为空！'));
            $password || die(json_error('密码不能为空！'));
            $model = M('business');
            $md5_password=md5_pwd($password,$username);
            $user = $model->where(array('business_mobile' => $username, 'business_password' => $md5_password))->find();
            if(!$user){
                addlog('密码或用户名错误', $username);
                die(json_error('密码或用户名错误,请重试!'));
            }
            if($user['is_del']==1){
                die(json_error('该商家已被查封,请连联系客服!'));
            }
            if($user['status']==0){
                 die(json_error('该用户正在审核中,请耐心等待!'));
            }else if($user['status']==1){
                $salt = C("COOKIE_SALT");
                $ip = get_client_ip();
                $ua = $_SERVER['HTTP_USER_AGENT'];
                session_start();
                session('uid',$user['id']);
                //加密cookie信息
                $auth = password($user['id'].$user['user_id'].$ip.$ua.$salt);
                if ($remember) {
                    cookie('auth', $auth, 3600 * 24 * 365);//记住我
                } else {
                    cookie('auth', $auth);
                }
                addlog('登录成功。');
                die(json_success('登录成功',['url'=>U('index/index')]));
            }else{
                die(json_error('审核未通过,请重新提交审核！'));
            }
               
        }



         $this->display();
    }

    function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }

    public function verify()
    {
        $config = array(
            'fontSize' => 14, // 验证码字体大小
            'length' => 4, // 验证码位数
            'useNoise' => false, // 关闭验证码杂点
            'imageW' => 100,
            'imageH' => 30,
        );
        $verify = new \Think\Verify($config);
        $verify->entry('login');
    }


}